package com.example.provider.service.impl;

import com.example.common.entity.BaseUser;
import com.example.common.entity.Message;
import com.example.provider.dao.BaseUserDao;
import com.example.provider.service.LoginAndRegeistService;
import com.example.provider.utils.JwtTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class LoginAndRegeistServiceImpl implements LoginAndRegeistService {
    @Autowired
    private BaseUserDao baseUserDao;

    @Autowired
    private JwtTokenUtil jwtTokenUtil;

    public Message regist(BaseUser u) {
        if( StringUtils.isEmpty(baseUserDao.findByUserName(u.getUserName()))){
            baseUserDao.insertUser(u);
            return new Message(800,"注册成功");

        } else{
            return new Message(300,"用户名存在");
        }
    }

    public Message login(BaseUser u) {
        BaseUser userBase= baseUserDao.findByUserName(u.getUserName());
        if( userBase.getPassword().equals(u.getPassword())){
            //拼接头像的地址
            String url = "http://localhost:8092/img/" + userBase.getPhotoUrl();
            userBase.setPhotoUrl(url);
            return  new Message(888,jwtTokenUtil.create(userBase),userBase);
        }
        else{
            return new Message(700,"密码错误");
        }
    }
}
